﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Authentication;
using System.Threading;
using System.ComponentModel;
using System.Security.Cryptography;
using AMS.Core.Data;
using AMS.Core.Services;
using AMS.Core.Cryptography;
using AMS.DD35.DbLoader.DataAccess;
using System.Runtime.Serialization;

namespace AMS.DD35.DbLoader.Models
{
    public class MasterModel
    {
        private string _sessionId;
        private DateTime _lastPing;
        
        // todo: move these to their own Models?
        #region Login Observer
        public event EventHandler LoginFailed;
        protected void RaiseLoginFailed(EventArgs e)
        {
            if (LoginFailed != null)
                LoginFailed(this, e);
        }

        public event EventHandler LoginSucceeded;
        protected void RaiseLoginSucceeded(EventArgs args)
        {
            if (LoginSucceeded != null)
                LoginSucceeded(this, args);
        }

        public event EventHandler LogoutSucceeded;
        protected void RaiseLogoutSucceeded(EventArgs args)
        {
            if (LogoutSucceeded != null)
                LogoutSucceeded(this, args);
        }
        #endregion

        #region Register Observer
        public event EventHandler RegistrationFailed;
        protected void RaiseRegisterFailed(EventArgs e)
        {
            if (RegistrationFailed != null)
                RegistrationFailed(this, e);
        }

        public event EventHandler RegistrationSucceeded;
        protected void RaiseRegisterSucceeded(EventArgs args)
        {
            if (RegistrationSucceeded != null)
                RegistrationSucceeded(this, args);
        }
        #endregion

        public MasterModel()
        { }

        public void Login(string userName, string password)
        {
            ILoginService _login = new LoginProxy();
            IHasher hasher = new Hasher();
            string pwHash = hasher.GetHash(password);

            try
            {
                _sessionId = Convert.ToString(_login.AttemptLogin(new LoginData(userName, pwHash)));
                RaiseLoginSucceeded(EventArgs.Empty);
            }
            catch (Exception ex)
            {
                RaiseLoginFailed(EventArgs.Empty);
            }
        }

        public void Register(string userName, string password, string email)
        {
            IRegisterService _register = new RegisterProxy();
            Hasher hash = new Hasher();
            string pwHash = hash.GetHash(password);

            try
            {
                _register.AttemptRegister(new RegistrationData(userName, pwHash, null, null, email));
                RaiseRegisterSucceeded(EventArgs.Empty);
            }
            catch (Exception)
            {
                RaiseRegisterFailed(EventArgs.Empty);
            }
        }

        public void Logout()
        {
            ILoginService _login = new LoginProxy();
            _login.Logout(_sessionId);
            RaiseLogoutSucceeded(EventArgs.Empty);
        }
    }
}
